package edu.clemson.wcbeck.cft;

//import java.io.File;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
//import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
//import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private List<MediaFile> mediaListImage;//, mediaListAudio;
    private MyDatabaseHelper dataSource;
    private List<Bitmap> mThumbs = new ArrayList<Bitmap>();

    public ImageAdapter(Context c) {
        mContext = c;
        
        dataSource = new MyDatabaseHelper(c);
        try {
			dataSource.createDataBase();
		} catch (IOException e) {
			String toastText = "Error creating the database!";
			Toast myToast = Toast.makeText(c, toastText, Toast.LENGTH_SHORT);
			myToast.show();
			e.printStackTrace();
		}
        
        dataSource.openDataBase();
        mediaListImage = dataSource.getAllMedia();
//        mediaListAudio = dataSource.getAllAudioMedia();
        dataSource.close();
        
        
        Iterator<MediaFile> iteratorImage = mediaListImage.iterator();
//        Iterator<MediaFile> iteratorAudio = mediaListAudio.iterator();
        
        while(iteratorImage.hasNext()){
        	MediaFile mediaFile = iteratorImage.next();
            setPic(mediaFile.getLocation(), mediaFile.getMediaType());
        }
        
//        while(iteratorAudio.hasNext())
//            setPic(iteratorAudio.next().getLocation());
    }

    public List<MediaFile> getMediaList(){
    	return mediaListImage;
    }
    
    public int getCount() {
        return mThumbs.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(5, 5, 5, 5);
        } else {
            imageView = (ImageView) convertView;
        }
        
        if (mThumbs.get(position) == null)
        	imageView.setImageResource(R.drawable.media_browser_icon);
        else
        	imageView.setImageBitmap(mThumbs.get(position));
        return imageView;
    }
    
    private void setPic(String mCurrentPhotoPath, int mediaType) {
    	Bitmap bitmap = null;
    	
		if (mediaType == 0) {
			File f1 = new File(mCurrentPhotoPath);
			// /* Decode the JPEG file into a Bitmap */
			bitmap = decodeFile(f1);
		}else if(mediaType == 1){
			
		}else if(mediaType == 2){
			bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.media_browser_icon);//decodeFile(f2);
		}
		
		mThumbs.add(bitmap);
	}
    
    private Bitmap decodeFile(File f) {
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            // The new size we want to scale to
            final int REQUIRED_SIZE = 200;

            // Find the correct scale value. It should be the power of 2.
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);

        } catch (FileNotFoundException e) {
        }
        return null;
    }
}